﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using NHibernate;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Controllers
{
    public class OrganizationController : Controller
    {
        public readonly OrganizationModel ObjOrganization;
        public readonly ProjectModel ObjProject;
        public readonly UsersModel ObjUsers;
        private ISession _session = NHibernateHelper.GetSession();

        public OrganizationController()
        {
            ObjOrganization = new OrganizationModel();
            ObjUsers = new UsersModel();
        }

        // 
        // GET: /Organization/ 
        [HttpGet]
        //[MvcSiteMapNode(Title = "Organizations", ParentKey = "Home")]
        public ActionResult Index()
        {
            IList<Organization> organizations = ObjOrganization.GetOrganizations();
            if (organizations.Count == 0)
            {
                return RedirectToAction("Index", "Initialize");
            }
            else
            {
                return View(organizations);
            }
        }

        // 
        // GET: /Organization/Details/
        [HttpGet]
        //[MvcSiteMapNode(Title = "Details", ParentKey = "Organizations", Key = "Organization")]
        public ActionResult Details(Guid id)
        {
            Organization organization = ObjOrganization.GetOrganizationById(id);
            //organization.ProjectCollection = new List<Project>();
            //IList<Project> List = ObjProject.GetProjects(id);
            //organization.ProjectCollection = (List<Project>) List;
            Client.Instance.CurrentOrganizationId = id;
            return View(organization);
        }

        // 
        // GET: /Organization/Create 
        [HttpGet]
        public ActionResult New()
        {
            var organization = new Organization();
            return View(organization);
        }

        // 
        // POST: /Organization/Create 
        [HttpPost]
        public ActionResult New(Organization organization)
        {
            try
            {
                organization.Users = ObjUsers.GetUsersById(Client.Instance.ClientId);
                ObjOrganization.NewOrganization(organization);
                Client.Instance.CurrentOrganizationId = organization.Id;
                if (ObjOrganization.GetOrganizations().Count == 0 || ObjOrganization.GetOrganizations().Count == 1)
                {
                    return RedirectToAction("Index", "Initialize");
                }
                else
                {
                    return RedirectToAction("Index", "Organization");
                }
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        // 
        // GET: /Organization/Edit/5 
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            Organization organization = ObjOrganization.GetOrganizationById(id);
            return View(organization);
        }

        // 
        // POST: /Organization/Edit/5 
        [HttpPost]
        public ActionResult Edit(Guid id, Organization organization)
        {
            try
            {
                organization.Users = ObjUsers.GetUsersById(Client.Instance.ClientId);
                ObjOrganization.UpdateOrganization(organization);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /Organization/Delete/5 
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            Organization organization = ObjOrganization.GetOrganizationById(id);
            return View(organization);
        }

        // 
        // POST: /Organization/Delete/5 
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                Organization organization = ObjOrganization.GetOrganizationById(id);
                organization.Users = ObjUsers.GetUsersById(Client.Instance.ClientId);
                ObjOrganization.DeleteOrganization(organization);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //private static string ErrorCodeToString(OrganizationValuesController createStatus)
        //{
        //    // See http://go.microsoft.com/fwlink/?LinkID=177550 for
        //    // a full list of status codes.
        //    switch (createStatus)
        //    {
        //        case OrganizationValuesController.:
        //            return "User name already exists. Please enter a different user name.";
        //        default:
        //            return
        //                "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
        //    }
        //}
    }
}